<?php
/**
 * Ultimate_ModuleCreator extension
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE_UMC.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 *
 * @category       Ultimate
 * @package        Ultimate_ModuleCreator
 * @copyright      Copyright (c) 2014
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 * @author         Marius Strajeru <ultimate.module.creator@gmail.com>
 */
/**
 * module edit template
 * @category    Ultimate
 * @package        Ultimate_ModuleCreator
 * @author         Marius Strajeru <marius.strajeru@gmail.com>
 */
?>
<script type="text/javascript">
    var nameAttributes = {};
</script>
<div class="content-header">
    <h3 class="icon-head"><?php echo $this->getHeaderText() ?></h3>
    <p class="content-buttons form-buttons">
        <?php echo $this->getBackButtonHtml() ?>
        <?php echo $this->getCancelButtonHtml() ?>
        <?php echo $this->getAddEntityButtonHtml();?>
        <?php echo $this->getSaveButtonHtml() ?>
        <?php echo $this->getSaveAndEditButtonHtml() ?>
    </p>
</div>
<form action="<?php echo $this->getSaveUrl() ?>" method="post" id="edit_form" enctype="multipart/form-data">
    <?php echo $this->getBlockHtml('formkey')?>
    <div style="display:none"></div>
</form>
<div id="entity_template" style="display:none">
    <?php echo $this->getChildHtml('entity-template');?>
</div>
<div id="attribute_template" style="display:none">
    <?php echo $this->getChildHtml('attribute-template');?>
</div>
<div id="menu_selector" style="display:none">
    <?php echo $this->getChildHtml('menu-selector');?>
</div>
<script type="text/javascript">
//<![CDATA[
    var moduleForm = new varienForm('edit_form', '<?php echo $this->getValidationUrl() ?>');
    moduleForm._processValidationResult = function(transport) {
        try{
            var response = transport.responseText.evalJSON();
            if (typeof varienGlobalEvents != undefined) {
                varienGlobalEvents.fireEvent('formValidateAjaxComplete', transport);
            }
            if (response.error){
                var errors = response.errors;
                var attributes = response.attributes;
                console.log(attributes);
                var globalErrors = '';
                if (response.message){
                    globalErrors = response.message;
                }
                if (attributes){
                    for (var i in attributes){
                        if (attributes.hasOwnProperty(i)){
                            if ($(i)){
                                $(i).setHasError(true, moduleForm);
                                Validation.ajaxError($(i), attributes[i]);
                                if (!Prototype.Browser.IE){
                                    $(i).focus();
                                }
                            }
                            else{
                                globalErrors += '<li>' + attributes[i] + '</li>';
                            }
                        }
                    }
                }
                if (globalErrors != '' && $('messages')){
                    $('messages').innerHTML = '<ul class="messages"><li class="error-msg"><ul>' + globalErrors + '</ul></li></ul>';
                    $('messages').show();
                    Effect.ScrollTo($('messages'));
                }
            }
            else{
                moduleForm._submit();
            }
        }
        catch (e){
            $('messages').innerHTML = '<ul class="messages"><li class="error-msg"><ul>' + transport.responseText + '</ul></li></ul>';
            $('messages').show();
            Effect.ScrollTo($('messages'));
        }
    };

    var templateSyntax = /(^|.|\r|\n)({{(\w+)}})/;
    function saveAndContinueEdit(urlTemplate) {
        var template = new Template(urlTemplate, templateSyntax);
        var url = template.evaluate({tab_id:modulecreator_info_tabsJsTabs.activeTab.id});
        var advices = $$('div.validation-advice');
        for (var i = 0; i<advices.length;i++){
            advices[i].remove();
        }
        if ($('messages')){
            $('messages').hide();
        }
        moduleForm.submit(url);
    }
    $('modulecreator_info_tabs_settings_content').select('table').each(function(el){
        decorateTable($(el));
    });
    var $form = '';
    document.observe("dom:loaded", function() {
        $form = new UMC.Module({
            relationSelect: '<?php echo $this->getRelationSelectTemplate()?>',
            entityTemplate: $('entity_template').innerHTML,
            attributeTemplate: $('attribute_template').innerHTML,
            nameAttributes: nameAttributes,
            collapsed: <?php echo $this->getShowFieldsetsCollapsed();?>
            <?php if ($this->getModule()) : ?>,
            relations: <?php echo $this->getModule()->getRelationsAsJson()?>
            <?php endif;?>
        });
    });
//]]>
</script>
